const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
module.exports = {
	target: 'web',
	entry: {
		commoncss: '/src/js/commoncss.js',
		flexible: '/src/js/flexible.js',
		dom: './src/js/common/dom.js',
		http: './src/js/common/http.js',
		utils: './src/js/common/utils.js',
		captcha: './src/lib/captcha-mini.js',
		index: './src/js/index.js',
		advertisement: './src/js/advertisement.js',
		login: './src/js/login.js',
		register: './src/js/register.js',
		my: './src/js/my.js',
		person: './src/js/person.js',
		run: './src/js/run.js',
		CourseTraining: './src/js/CourseTraining.js',
		CourseDetails: './src/js/CourseDetails.js',
		player: './src/js/player.js',
	},
	output: {
		path: path.resolve(__dirname, 'dist'),
		filename: 'js/[name].js',
		publicPath: './',
	},
	module: {
		rules: [
			{
				test: /\.css$/,
				use: [
					{
						loader: MiniCssExtractPlugin.loader,
						options: {
							publicPath: '../',
						},
					},
					'css-loader',
				],
			},
			{
				test: /\.less$/,
				use: [
					{
						loader: MiniCssExtractPlugin.loader,
						options: {
							publicPath: '../',
						},
					},
					'css-loader',
					'less-loader',
				],
			},
			{
				test: /\.(jpg|png|gif)$/,
				loader: 'url-loader',
				options: {
					limit: 10 * 1024,
					name: '[hash:16].[ext]',
					esModule: false,
					outputPath: 'img',
				},
			},
			{
				test: /\.html$/,
				loader: 'html-loader',
			},
			{
				test: /\.(eot|ttf|woff|woff2)$/,
				loader: 'file-loader',
				options: {
					name: '[hash:16].[ext]',
					outputPath: 'fonts',
				},
			},
			{
				test: /\.js$/,
				exclude: /node_modules/,
				loader: 'babel-loader',
			},
		],
	},
	plugins: [
		new HtmlWebpackPlugin({
			template: './src/index.html',
			filename: 'index.html',
			chunks: ['commoncss', 'flexible', 'index', 'http', 'utils'],
		}),
		new HtmlWebpackPlugin({
			template: './src/advertisement.html',
			filename: 'advertisement.html',
			chunks: ['commoncss', 'flexible', 'advertisement'],
		}),
		new HtmlWebpackPlugin({
			template: './src/login.html',
			filename: 'login.html',
			chunks: ['commoncss', 'flexible', 'login', 'http', 'utils'],
		}),
		new HtmlWebpackPlugin({
			template: './src/register.html',
			filename: 'register.html',
			chunks: [
				'commoncss',
				'flexible',
				'captcha',
				'http',
				'register',
				'utils',
			],
		}),
		new HtmlWebpackPlugin({
			template: './src/my.html',
			filename: 'my.html',
			chunks: ['commoncss', 'flexible', 'captcha', 'http', 'my', 'utils'],
		}),
		new HtmlWebpackPlugin({
			template: './src/person.html',
			filename: 'person.html',
			chunks: [
				'commoncss',
				'flexible',
				'captcha',
				'http',
				'person',
				'utils',
			],
		}),
		new HtmlWebpackPlugin({
			template: './src/run.html',
			filename: 'run.html',
			chunks: [
				'commoncss',
				'flexible',
				'captcha',
				'http',
				'run',
				'utils',
			],
		}),
		new HtmlWebpackPlugin({
			template: './src/CourseTraining.html',
			filename: 'CourseTraining.html',
			chunks: [
				'commoncss',
				'flexible',
				'captcha',
				'http',
				'CourseTraining',
				'utils',
			],
		}),
		new HtmlWebpackPlugin({
			template: './src/CourseDetails.html',
			filename: 'CourseDetails.html',
			chunks: [
				'commoncss',
				'flexible',
				'captcha',
				'http',
				'CourseDetails',
				'utils',
			],
		}),
		new HtmlWebpackPlugin({
			template: './src/player.html',
			filename: 'player.html',
			chunks: [
				'commoncss',
				'flexible',
				'captcha',
				'http',
				'player',
				'utils',
			],
		}),
		new MiniCssExtractPlugin({
			filename: 'css/[name].css',
		}),
		new OptimizeCssAssetsPlugin(),
		new CleanWebpackPlugin(),
	],
	mode: process.env.NODE_ENV,
	devServer: {
		contentBase: path.resolve(__dirname, 'dist'),
		compress: true,
		port: 666,
		open: true,
		publicPath: '/',
		openPage: 'index.html',
	},
}
